package com.java.test.system.gametypemanage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.test.dto.PageResultDTO;
import com.java.test.dto.ResponseDTO;
import com.java.test.system.gametypemanage.constant.GameTypeResponseCodeConst;
import com.java.test.system.gametypemanage.dto.GameTypeAddDTO;
import com.java.test.system.gametypemanage.dto.GameTypeQueryDTO;
import com.java.test.system.gametypemanage.dto.GameTypeUpdateDTO;
import com.java.test.system.gametypemanage.entity.GameTypeEntity;
import com.java.test.timer.MessagePushTimer;
import com.java.test.util.SmartBeanUtil;
import com.java.test.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 游戏类型Service
 * @author lijie1
 */
@Service
public class GameTypeManageService {

    @Autowired
    private GameTypeManageDAO gameTypeManageDAO;

    /**
     * 获取游戏分类列表
     * @param queryDTO
     * @return
     */
    public ResponseDTO<PageResultDTO<GameTypeEntity>> getGameTypeList(GameTypeQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<GameTypeEntity> entityList = gameTypeManageDAO.selectGameTypeList(queryDTO);
        PageResultDTO<GameTypeEntity> pageResultDTO = SmartPageUtil.convert2PageResult(page, entityList, GameTypeEntity.class);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 添加游戏类型
     * @param addDTO
     * @return
     */
    public ResponseDTO<String> addGameType(GameTypeAddDTO addDTO) {
        GameTypeEntity entity = gameTypeManageDAO.getByGName(addDTO.getName());
        if (entity != null) {
            return ResponseDTO.wrap(GameTypeResponseCodeConst.ALREADY_EXIST);
        }
        int i = gameTypeManageDAO.insertGameType(addDTO);
        if(i>0){
            MessagePushTimer.gameTypeCLQ.offer("gameTypePush");
        }
        return ResponseDTO.succ();
    }

    /**
     * 查询游戏类型BYid
     * @param id
     * @return
     */
    public ResponseDTO<GameTypeEntity> selectById(String id) {
        GameTypeEntity entity = gameTypeManageDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(GameTypeResponseCodeConst.NOT_EXIST);
        }
        GameTypeEntity configDTO = SmartBeanUtil.copy(entity, GameTypeEntity.class);
        return ResponseDTO.succData(configDTO);
    }

    /**
     * 修改游戏类型
     * @param updateDTO
     * @return
     */
    public ResponseDTO<String> updateGameType(GameTypeUpdateDTO updateDTO) {
        GameTypeEntity entity = gameTypeManageDAO.getById(updateDTO.getId());
        //游戏类型不存在
        if (entity == null) {
            return ResponseDTO.wrap(GameTypeResponseCodeConst.NOT_EXIST);
        }
        GameTypeEntity alreadyEntity = gameTypeManageDAO.getByKeyExcludeId(updateDTO.getName(), updateDTO.getId());
        if (alreadyEntity != null) {
            return ResponseDTO.wrap(GameTypeResponseCodeConst.ALREADY_EXIST);
        }
        int i = gameTypeManageDAO.updateById(updateDTO);
        if(i>0){
            MessagePushTimer.gameTypeCLQ.offer("gameTypePush");
        }
        return ResponseDTO.succ();
    }

    /**
     * 删除游戏类型
     * @param id
     * @return
     */
    public ResponseDTO<String> deleteById(String id) {
        GameTypeEntity entity = gameTypeManageDAO.getById(id);
        //游戏类型不存在
        if (entity == null) {
            return ResponseDTO.wrap(GameTypeResponseCodeConst.NOT_EXIST);
        }
        int i = gameTypeManageDAO.deleteById(id);
        if(i>0){
            MessagePushTimer.gameTypeCLQ.offer("gameTypePush");
        }
        return ResponseDTO.succ();
    }
}
